from rest_framework import serializers
from .models import Idc

class IDCSerializer(serializers.ModelSerializer):
    class Meta:
        model = Idc
        fields = "__all__"


class IdcSerializers(serializers.Serializer):
    id = serializers.IntegerField(read_only=True,help_text="ID")
    name = serializers.CharField(required=False,help_text="IDC名称",label="IDC名称")
    address = serializers.CharField(required=False,help_text="IDC地址",label="IDC地址")
    phone = serializers.CharField(required=False,help_text="IDC电话",label="IDC电话")
    email = serializers.EmailField(required=False,help_text="IDC邮箱",label="IDC邮箱")
    test = serializers.CharField(read_only=True,required=False,label="aaa")

    def create(self, validated_data):
        return Idc.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.name = validated_data.get("name",instance.name)
        instance.address = validated_data.get("address",instance.address)
        instance.phone = validated_data.get("phone",instance.phone)
        instance.email = validated_data.get("email",instance.email)
        instance.save()
        return instance

    def to_internal_value(self, data):
        #这是序列化的第一步
        return super(IdcSerializers, self).to_internal_value(data)

    def to_representation(self, instance):
        #这是反序列化的最后一步
        ret = super(IdcSerializers, self).to_representation(instance)
        return ret
        
    #data = {"name": "jifang4", "address": "beijing", "phone": "2133455667", "email":"jifang4 @ baidu.com"}